package org.dtrd.modules.dict.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.dict.entity.bean.BankInfoDetail;
import org.dtrd.modules.dict.entity.bean.BankInfoListInfo;
import org.dtrd.modules.dict.entity.request.BankInfoListRequest;
import org.dtrd.modules.dict.service.IDtrdEntRdDicBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author QR
 * @since 2021-12-13
 */
@Api(tags = "银行相关")
@RestController
@RequestMapping("/dict/bank/")
public class BankInfoController extends BaseController {

    @Autowired
    private IDtrdEntRdDicBankService bankInfoService;

    @ApiOperation(value = "查询银行详情", response = BankInfoDetail.class)
    @GetMapping("info")
    @AutoLog(value="查询详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getBankInfo(@RequestParam Integer id){
        BankInfoDetail info = bankInfoService.getBankInfo(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询银行列表", response = BankInfoListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getBankInfoList(@RequestBody BankInfoListRequest request){
        List<BankInfoListInfo> list = bankInfoService.getBankInfoList(request);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改银行信息")
    @PostMapping("edit")
    @AutoLog(value="新增或修改银行信息", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateBankInfo(@RequestBody BankInfoDetail dto){
        boolean result = bankInfoService.saveOrUpdateBankInfo(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除银行信息")
    @DeleteMapping("remove")
    @AutoLog(value="删除银行信息", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeBankInfo(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = bankInfoService.removeBankInfo(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
